# -*- coding: utf-8 -*-
import time

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from mdp_random import data_handle,random_forest
from mdp_dnn import nerual_network
from mdp_SVM import Support_Vector_Machine
from mdp_LDA import LDA_algorithm
from mdp_knn import KNN_algorithm
from mdp_all import mdp_data
from test_random import test_random, random_result
from test_dnn import test_network, dnn_result
from test_SVM import test_SVM, svm_result
from test_LDA import test_LDA, lda_result
from childWindow import  *
from mdp_DT import Decision_Tree
from test_DT import DT_result, test_DT
from test_all import test_data
from test_knn import knn_result, test_knn


class Ui_Form(QWidget):
    filename_train=""
    filename_test=""
    def __init__(self):
        super(Ui_Form,self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)

    def setupUi(self, Form):
        Form.setObjectName("软件缺陷预测系统")
        Form.resize(650, 450)
        self.tabWidget = QtWidgets.QTabWidget(Form)
        self.tabWidget.setGeometry(QtCore.QRect(30, 20, 600, 400))
        self.tabWidget.setObjectName("tabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        # 创建启动界面，支持png透明图片
        splash = QtWidgets.QSplashScreen(QtGui.QPixmap('pic/pic.png'))
        splash.setGeometry(700,320,500,500)
        splash.show()
        time.sleep(1)
        # 可以显示启动信息
        splash.showMessage('正在加载……')
        self.pushButton_2 = QtWidgets.QPushButton(self.tab)
        self.pushButton_2.setGeometry(QtCore.QRect(40, 280, 120, 40))
        self.pushButton_2.setObjectName("pushButton_2")
        self.label = QtWidgets.QLabel(self.tab)
        self.label.setGeometry(QtCore.QRect(40, 40, 171, 16))
        self.label.setObjectName("label")
        self.cf = QtWidgets.QPushButton(self.tab)
        self.cf.setGeometry(QtCore.QRect(200, 30, 93, 28))
        self.cf.setObjectName("cf")
        self.label_6 = QtWidgets.QLabel(self.tab)
        self.label_6.setGeometry(QtCore.QRect(300, 40, 350, 16))
        self.label_6.setText("")
        self.label_6.setObjectName("label_6")
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.label_11 = QtWidgets.QLabel(self.tab_2)
        self.label_11.setGeometry(QtCore.QRect(10, 30, 191, 16))
        self.label_11.setObjectName("label_11")
        self.label_12 = QtWidgets.QLabel(self.tab_2)
        self.label_12.setGeometry(QtCore.QRect(250, 30, 271, 16))
        self.label_12.setObjectName("label_12")
        self.cf2 = QtWidgets.QPushButton(self.tab_2)
        self.cf2.setGeometry(QtCore.QRect(250, 60, 93, 28))
        self.cf2.setObjectName("cf2")
        self.pushButton = QtWidgets.QPushButton(self.tab_2)
        self.pushButton.setGeometry(QtCore.QRect(250, 100, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.label_13 = QtWidgets.QLabel(self.tab_2)
        self.label_13.setGeometry(QtCore.QRect(40, 210, 111, 16))
        self.label_13.setObjectName("label_13")
        self.label_14 = QtWidgets.QLabel(self.tab_2)
        self.label_14.setGeometry(QtCore.QRect(150, 210, 31, 16))
        self.label_14.setObjectName("label_14")
        self.label_15 = QtWidgets.QLabel(self.tab_2)
        self.label_15.setGeometry(QtCore.QRect(40, 240, 121, 16))
        self.label_15.setObjectName("label_15")
        self.label_16 = QtWidgets.QLabel(self.tab_2)
        self.label_16.setGeometry(QtCore.QRect(160, 240, 31, 16))
        self.label_16.setObjectName("label_16")
        self.label_17 = QtWidgets.QLabel(self.tab_2)
        self.label_17.setGeometry(QtCore.QRect(40, 280, 261, 16))
        self.label_17.setObjectName("label_17")
        self.pushButton_5 = QtWidgets.QPushButton(self.tab_2)
        self.pushButton_5.setGeometry(QtCore.QRect(170, 280, 93, 31))
        self.pushButton_5.setObjectName("pushButton_5")
        self.radioButton = QtWidgets.QRadioButton(self.tab)
        self.radioButton.setGeometry(QtCore.QRect(30, 70, 115, 19))
        self.radioButton.setObjectName("radioButton")
        self.radioButton_2 = QtWidgets.QRadioButton(self.tab)
        self.radioButton_2.setGeometry(QtCore.QRect(30, 100, 151, 19))
        self.radioButton_2.setObjectName("radioButton_2")
        self.radioButton_3 = QtWidgets.QRadioButton(self.tab)
        self.radioButton_3.setGeometry(QtCore.QRect(30, 130, 151, 19))
        self.radioButton_3.setObjectName("radioButton_3")
        self.radioButton_lda1 = QtWidgets.QRadioButton(self.tab)
        self.radioButton_lda1.setGeometry(QtCore.QRect(30, 160, 151, 19))
        self.radioButton_lda1.setObjectName("radioButton_lda1")
        self.radioButton_DT1 = QtWidgets.QRadioButton(self.tab)
        self.radioButton_DT1.setGeometry(QtCore.QRect(30, 190, 151, 19))
        self.radioButton_DT1.setObjectName("radioButton_DT1")
        self.radioButton_KNN1 = QtWidgets.QRadioButton(self.tab)
        self.radioButton_KNN1.setGeometry(QtCore.QRect(30, 220, 151, 19))
        self.radioButton_KNN1.setObjectName("radioButton_KNN1")

        self.radioButton_4 = QtWidgets.QRadioButton(self.tab_2)
        self.radioButton_4.setGeometry(QtCore.QRect(40, 70, 151, 19))
        self.radioButton_4.setObjectName("radioButton_4")
        self.radioButton_5 = QtWidgets.QRadioButton(self.tab_2)
        self.radioButton_5.setGeometry(QtCore.QRect(40, 100, 151, 19))
        self.radioButton_5.setObjectName("radioButton_5")
        self.radioButton_6 = QtWidgets.QRadioButton(self.tab_2)
        self.radioButton_6.setGeometry(QtCore.QRect(40, 130, 151, 19))
        self.radioButton_6.setObjectName("radioButton_6")
        self.radioButton_LDA2 = QtWidgets.QRadioButton(self.tab_2)
        self.radioButton_LDA2.setGeometry(QtCore.QRect(40, 160, 151, 19))
        self.radioButton_LDA2.setObjectName("radioButton_LDA2")
        self.radioButton_DT2 = QtWidgets.QRadioButton(self.tab_2)
        self.radioButton_DT2.setGeometry(QtCore.QRect(40, 190, 151, 19))
        self.radioButton_DT2.setObjectName("radioButton_DT2")
        self.radioButton_KNN2 = QtWidgets.QRadioButton(self.tab_2)
        self.radioButton_KNN2.setGeometry(QtCore.QRect(40, 220, 151, 19))
        self.radioButton_KNN2.setObjectName("radioButton_KNN2")

        self.radioButton_test_all = QtWidgets.QRadioButton(self.tab_2)
        self.radioButton_test_all.setGeometry(QtCore.QRect(40, 250, 151, 19))
        self.radioButton_test_all.setObjectName("radioButton_test_all")
        self.radioButton_all = QtWidgets.QRadioButton(self.tab)
        self.radioButton_all.setGeometry(QtCore.QRect(30, 250, 151, 19))
        self.radioButton_all.setObjectName("radioButton_all")
        self.label_7 = QtWidgets.QLabel(self.tab_2)
        self.label_7.setGeometry(QtCore.QRect(350, 65, 350, 16))
        self.label_7.setText("")
        self.label_7.setObjectName("label_7")
        self.tabWidget.addTab(self.tab_2, "")

        QtCore.QMetaObject.connectSlotsByName(Form)
        self.cf.clicked.connect(self.openfile)
        self.cf2.clicked.connect(self.openfile2)
        self.pushButton.clicked.connect(self.test)
        self.pushButton_2.clicked.connect(self.train)
        self.pushButton_5.clicked.connect(self.view_report)
        self.retranslateUi(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "软件缺陷预测系统"))
        self.pushButton_2.setText(_translate("Form", "模型训练"))
        self.label.setText(_translate("Form", "上传软件缺陷数据集："))
        self.cf.setText(_translate("Form", "选择文件"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Form", "模型训练"))
        self.label_11.setText(_translate("Form", "选择预测模型："))
        self.radioButton.setText(_translate("Form", "随机森林模型"))
        self.radioButton_2.setText(_translate("Form", "深度神经网络模型"))
        self.radioButton_3.setText(_translate("Form", "SVM模型"))
        self.radioButton_lda1.setText(_translate("Form", "LDA算法"))
        self.radioButton_DT1.setText(_translate("Form", "决策树算法"))
        self.radioButton_KNN1.setText(_translate("Form", "KNN算法"))

        self.radioButton_4.setText(_translate("Form", "随机森林模型"))
        self.radioButton_5.setText(_translate("Form", "深度神经网络模型"))
        self.radioButton_6.setText(_translate("Form", "SVM模型"))
        self.radioButton_LDA2.setText(_translate("Form", "LDA算法"))
        self.radioButton_DT2.setText(_translate("Form", "决策树算法"))
        self.radioButton_KNN2.setText(_translate("Form", "KNN算法"))

        self.radioButton_all.setText(_translate("Form", "同时训练"))
        self.radioButton_test_all.setText(_translate("Form", "同时预测"))
        self.label_12.setText(_translate("Form", "上传需要预测的软件缺陷集："))
        self.cf2.setText(_translate("Form", "选择文件"))
        self.pushButton.setText(_translate("Form", "开始预测"))

        self.label_17.setText(_translate("Form", "保存预测报告:"))
        self.pushButton_5.setText(_translate("Form", "保存"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Form", "缺陷预测"))

    def train(self):
        print(self.filename_train)
        if self.radioButton.isChecked():
            random_forest(self.filename_train)
        elif self.radioButton_2.isChecked():
            nerual_network(self.filename_train)
        elif self.radioButton_3.isChecked():
            Support_Vector_Machine(self.filename_train)
        elif self.radioButton_lda1.isChecked():
            LDA_algorithm(self.filename_train)
        elif self.radioButton_DT1.isChecked():
            Decision_Tree(self.filename_train)
        elif self.radioButton_KNN1.isChecked():
            KNN_algorithm(self.filename_train)
        elif self.radioButton_all.isChecked():
            random_forest(self.filename_train,1)
            nerual_network(self.filename_train,1)
            Support_Vector_Machine(self.filename_train,1)
            LDA_algorithm(self.filename_train,1)
            Decision_Tree(self.filename_train,1)
            mdp_data.plot_roc_all(mdp_data)


    def test(self):
        print(self.filename_test)
        if self.radioButton_4.isChecked():
            random_result(self.filename_test)
        elif self.radioButton_5.isChecked():
            dnn_result(self.filename_test)
        elif self.radioButton_6.isChecked():
            svm_result(self.filename_test)
        elif self.radioButton_LDA2.isChecked():
            lda_result(self.filename_test)
        elif self.radioButton_DT2.isChecked():
            DT_result(self.filename_test)
        elif self.radioButton_KNN2.isChecked():
            knn_result(self.filename_test)
        elif self.radioButton_test_all.isChecked():
            random_result(self.filename_test,1)
            dnn_result(self.filename_test,1)
            svm_result(self.filename_test,1)
            lda_result(self.filename_test,1)
            DT_result(self.filename_test,1)
            knn_result(self.filename_test,1)
            test_data.plot_roc_all(test_data)

    def view_report(self):
        if self.radioButton_4.isChecked():
            test_random(self.filename_test)
        elif self.radioButton_5.isChecked():
            test_network(self.filename_test)
        elif self.radioButton_6.isChecked():
            test_SVM(self.filename_test)
        elif self.radioButton_LDA2.isChecked():
            test_LDA(self.filename_test)
        elif self.radioButton_DT2.isChecked():
            test_DT(self.filename_test)
        elif self.radioButton_KNN2.isChecked():
            test_knn(self.filename_test)

    def openfile(self):
        openfile_name, filetype = QFileDialog.getOpenFileName(self,'选择文件', "/", "All Files (*);;Excel files (*.xlsx , *.xls , *.csv)")
        self.label_6.setText("已选择")
        self.filename_train=openfile_name

    def openfile2(self):
        openfile_name, filetype = QFileDialog.getOpenFileName(self,'选择文件', "/", "All Files (*);;Excel files (*.xlsx , *.xls , *.csv)")
        self.label_7.setText("已选择")
        self.filename_test=openfile_name

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    #实例化主窗口
    Widgets = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Widgets)

    #实例化子窗口
    child = QDialog()
    child_ui = Ui_Dialog()
    child_ui.setupUi(child)

    # 按钮绑定事件
    btn = ui.pushButton_5
    btn.clicked.connect(child.show)
    Widgets.show()
    sys.exit(app.exec_())

